import time

from appium.options.android import UiAutomator2Options
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By


class CommonDriver:
    def get_driver(self):
        options = UiAutomator2Options()
        options.platform_name = "Android"
        options.platform_version = "7"
        options.app_package = "com.microvirt.launcher2"
        options.app_activity = "com.microvirt.launcher.Launcher"
        options.no_reset = False
        options.unicode_keyboard = True
        options.udid = "127.0.0.1:21513"

        command_executor_url = "http://127.0.0.1:4723/wd/hub"

        driver = webdriver.Remote(command_executor=command_executor_url, options=options)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    # 找到当当网图标
    dangdang = driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@text="当当"]')
    # 使driver能够拥有更多的移动端操作函数
    action = TouchAction(driver)
    # 长按当当图标移动到对应的坐标处
    action.long_press(dangdang).move_to(x=92, y=605).release().perform()
